//
//  GLPickerViewController.m
//  KTools
//
//  Created by zhiyong.kuang on 2017/10/20.
//  Copyright © 2017年 zhiyong.kuang. All rights reserved.
//

#import "GLPickerViewController.h"
#import "GLPickerView.h"
#import "GLCustomCell.h"
#import "UIViewController+GLForm.h"


@interface GLPickerViewController ()

@end

@implementation GLPickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

    [self loadForm];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark-

#pragma mark- UITableViewDataSource and UITableViewDelegate
-(void)showPickerByType:(GLFormRow*)row{
    if ([row.tag isEqualToString:@"data1"]) {
        NSArray* datas = @[@"数据1",@"数据2",@"数据3",@"数据4",@"数据5",@"数据1",@"数据6",@"数据7",@"数据8",@"数据9",@"数据10",@"数据11",@"数据12",@"数据13",@"数据14",@"数据15",@"数据16",];
        GLPickerView* picker = [GLPickerView showDatas:datas convert:nil competion:^(GLPickerEvent event, id value) {
            if (event == GLPickerEventSure) {
                [row setRowValue:value];
            }
            
        }];
        picker.titleLabel.text = @"选择单列数据";
        picker.titleLabel.backgroundColor = [UIColor redColor];
    }
    else if ([row.tag isEqualToString:@"data2"]) {
        NSString* path = [[NSBundle mainBundle]pathForResource:@"city.json" ofType:nil];
        NSData* data = [NSData dataWithContentsOfFile:path];
        NSArray* datas = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSLog(@"");
        GLPickerView* picker = [GLPickerView showDatas:datas component:3 weights:@[@1,@1,@1] subArrayKey:@[@"cities",@"cities"] convert:^NSString *(id model, NSInteger component) {
            NSDictionary* m = model;
            if (component == 0) {
                return m[@"name"];
            }
            else if (component == 1){
                return m[@"name"];
            }
            else if (component == 2){
                return m[@"name"];
            }
            return @"";
        } competion:^(GLPickerEvent event, NSArray *values) {
            if (event == GLPickerEventSure) {
                NSDictionary* dict1 = values[0];
                NSDictionary* dict2 = values[1];
                NSDictionary* dict3 = values[2];
                NSString* str = [NSString stringWithFormat:@"%@ %@ %@",dict1[@"name"],dict2[@"name"],dict3[@"name"]];
                [row setRowValue:str];
            }
            
        }];
        picker.titleLabel.text = @"选择城市";
    }
    else if ([row.tag isEqualToString:@"data3"]) {

        [GLPickerView showCustomePickerByWeights:@[@1,@1] numOfComponent:^NSInteger(UIPickerView *picker) {
            return 2;
        } numOfRows:^NSInteger(UIPickerView *picker, NSInteger component) {
            return 88;
        } titleView:^UIView *(UIPickerView *picker, NSInteger row, NSInteger component, UILabel *label) {
            label.text = [NSString stringWithFormat:@"%ld===%ld",component,row];
            return label;
        } didSelected:^(UIPickerView *picker, NSInteger row2, NSInteger component) {
            [row setRowValue:[NSString stringWithFormat:@"%ld===%ld",component,row2 ]];
        } competion:^(GLPickerEvent event, id value) {
            
        }];
    }
    else if ([row.tag isEqualToString:@"data4"]) {
        NSMutableArray* array1 = [NSMutableArray arrayWithCapacity:0];
        NSMutableArray* array2 = [NSMutableArray arrayWithCapacity:0];
        for(NSInteger index = 0;index<100;index++){
            [array1 addObject:[NSString stringWithFormat:@"数据%ld",index]];
            [array2 addObject:[NSString stringWithFormat:@"第二列%ld",index]];
        }
        [GLPickerView showNoLinkageDatas:@[array1,array2] component:2 weights:@[@1,@1] convert:nil competion:^(GLPickerEvent event, NSArray *values) {
            if (event == GLPickerEventSure) {
                [row setRowValue:[NSString stringWithFormat:@"%@=%@",values[0],values[1]]];
            }
            
        }];
    }
    else{
        NSIndexPath* indexPath = [self.gl_form indexPathForFormRow:row];
        NSDate* minDate = [GLDatePickerView setYear:1988 month:9 day:6 hour:9 minute:22 second:22];
        NSDate* maxDate = [GLDatePickerView setYear:2027 month:9 day:6 hour:18 minute:30 second:33];
//        NSDate* curDate = [GLDatePickerView setYear:2008 month:9 day:1 hour:00 minute:00 second:00];
        GLDatePickerView* picker = [GLDatePickerView showDate:indexPath.row minDate:minDate maxDate:maxDate selectdDate:nil competion:^(GLPickerEvent event, NSDateComponents *value) {
            if (event == GLPickerEventSure) {
                NSString* time = [NSString stringWithFormat:@"%ld-%02ld-%02ld %02ld:%02ld:%02ld",value.year,value.month,value.day,value.hour,value.minute,value.second];
                [row setRowValue:time];
            }
            
        }];
        picker.titleLabel.text = @"选择城市";
        picker.titleLabel.textColor = [UIColor orangeColor];
        [picker.cancleBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [picker.sureBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        picker.header.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.4];
        picker.headerLine.backgroundColor = [UIColor redColor];
    }
    
    
}

#pragma mark- getter and setter
-(void)loadForm{
    //GLButtonViewController
    GLForm* form = [[GLForm alloc]init];
    
    GLFormSection* section = [[GLFormSection alloc]init];
    
    for (NSInteger index = 0;index < GLDatePickerModeMaxNumber;index++) {
        GLFormRow* row00 = [GLFormRow formRowByComponentKeys:@[GLCustomCellIcon,GLCustomCellText,GLCustomCellCatpionText,GLCustomCellArrow] withTag:[NSString stringWithFormat:@"date%ld",index]];
        [row00 addConfigValue:@"icon" forKeyPath:GLCustomCellIcon];
        [row00 addConfigValue:[NSString stringWithFormat:@"Mode:%ld",index] forKeyPath:GLCustomCellText];
        row00.valueKey = GLCustomCellCatpionText;
        [section addFormRow:row00];
    }
    
    GLFormRow* row1 = [GLFormRow formRowByComponentKeys:@[GLCustomCellIcon,GLCustomCellText,GLCustomCellCatpionText,GLCustomCellArrow] withTag:@"data1"];
    [row1 addConfigValue:@"icon" forKeyPath:GLCustomCellIcon];
    [row1 addConfigValue:@"单列数据" forKeyPath:GLCustomCellText];
    row1.valueKey = GLCustomCellCatpionText;
    [section addFormRow:row1];
    
    GLFormRow* row2 = [GLFormRow formRowByComponentKeys:@[GLCustomCellIcon,GLCustomCellText,GLCustomCellCatpionText,GLCustomCellArrow] withTag:@"data2"];
    [row2 addConfigValue:@"icon" forKeyPath:GLCustomCellIcon];
    [row2 addConfigValue:@"选择城市" forKeyPath:GLCustomCellText];
    row2.valueKey = GLCustomCellCatpionText;
    [section addFormRow:row2];
    
    GLFormRow* row3 = [GLFormRow formRowByComponentKeys:@[GLCustomCellIcon,GLCustomCellText,GLCustomCellCatpionText,GLCustomCellArrow] withTag:@"data3"];
    [row3 addConfigValue:@"icon" forKeyPath:GLCustomCellIcon];
    [row3 addConfigValue:@"自定义picker" forKeyPath:GLCustomCellText];
    row3.valueKey = GLCustomCellCatpionText;
    [section addFormRow:row3];
    
    GLFormRow* row4 = [GLFormRow formRowByComponentKeys:@[GLCustomCellIcon,GLCustomCellText,GLCustomCellCatpionText,GLCustomCellArrow] withTag:@"data4"];
    [row4 addConfigValue:@"icon" forKeyPath:GLCustomCellIcon];
    [row4 addConfigValue:@"非联动picker" forKeyPath:GLCustomCellText];
    row4.valueKey = GLCustomCellCatpionText;
    [section addFormRow:row4];
    
    
    
    [form addFormSection:section];
    
    __weak typeof(self)weakSelf = self;
    form.formClicked = ^(GLFormRow *row, id sender, BOOL isSub, NSString *subTag) {
        [weakSelf showPickerByType:row];
    };
    [self gl_registerForm:form];
    
}


@end
